#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TraineeProjectProvider : CY.GFive.Core.Providers.DALProvider.ITraineeProjectProvider
	{
		#region Sql
		private static readonly string SqlSelectTraineeProject = "SELECT [Id], [StdCode],[ProjectCode] FROM [TraineeProject] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTraineeProject = "INSERT INTO [TraineeProject]([StdCode],[ProjectCode]) values(@StdCode,@ProjectCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTraineeProject = "UPDATE [TraineeProject] SET [StdCode] = @StdCode,[ProjectCode] = @ProjectCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTraineeProject = "DELETE [TraineeProject] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTraineeProject = "SELECT [Id], [StdCode],[ProjectCode] FROM [TraineeProject]";
		#endregion

		#region ITraineeProjectProvider Members
		public Core.Business.TraineeProject Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTraineeProject);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TraineeProject traineeProject = new Core.Business.TraineeProject();

				if (!reader.IsDBNull(0)) traineeProject.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) traineeProject.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) traineeProject.ProjectCode = reader.GetString(2);

				return traineeProject;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TraineeProject traineeProject)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, traineeProject.StdCode);
			sql.AddParameter("@ProjectCode", SqlDbType.NVarChar, traineeProject.ProjectCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTraineeProject);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) traineeProject.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TraineeProject traineeProject)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, traineeProject.StdCode);
			sql.AddParameter("@ProjectCode", SqlDbType.NVarChar, traineeProject.ProjectCode);
			sql.AddParameter("@Id", SqlDbType.Int, traineeProject.Id);
			sql.ExecuteSql(SqlUpdateTraineeProject);
		}

		public void Delete(Core.Business.TraineeProject traineeProject)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, traineeProject.Id);
			sql.ExecuteSql(SqlDeleteTraineeProject);
		}

		public IList<Core.Business.TraineeProject> GetAllTraineeProject()
		{
			IList<Core.Business.TraineeProject> traineeProjectlist = new List<Core.Business.TraineeProject>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTraineeProject);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TraineeProject traineeProject = new Core.Business.TraineeProject();

					if (!reader.IsDBNull(0)) traineeProject.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) traineeProject.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) traineeProject.ProjectCode = reader.GetString(2);

					traineeProject.MarkOld();
					traineeProjectlist.Add(traineeProject);
				}
				reader.Close();
			}
			return traineeProjectlist;
		}
		#endregion
	}
}
